<template>
  <div class="sorts-container">
    <el-form class="top-form" :model="formParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="sortName">
        <el-input
          v-model="formParams.sortName"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主表名称" prop="sortMainTableNo">
        <el-select multiple v-model="formParams.sortMainTableNo" placeholder="请选择主表名称">
          <el-option
            v-for="(item,index) in tableOptions"
            :key="index"
            :label="item.tableName"
            :value="item.recNo"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="bottom-main">
      <div class="left-tree">
        <el-tree
          :data="treeData"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          highlight-current
          @node-click="handleNodeClick"
        />
      </div>

      <div class="right-table">
        <rightBtns
          v-if="isUnderlying !== '0'"
          :handleAdd="handleAdd"
          :handleUpdate="handleUpdate"
          :handleDelete="handleDelete"
          :handleExport="handleExport"
          :showSearch="showSearch"
          :getList="getList"
          :multiple="multiple"
          :single="single"
          :addAvail="addAvail"
        />
        <commonTables
          v-if="isUnderlying === '1' || !isUnderlying"
          :sortList="sortList"
          :handleSelectionChange="handleSelectionChange"
          :handleUpdate="handleUpdate"
          :handleDelete="handleDelete"
        />
        <lastTables
          v-loading="lastTableLoading"
          v-else-if="isUnderlying !== '0'"
          :sortList="lastSortList"
          :tableLabel="tableLabel"
          :handleSelectionChange="handleSelectionChange"
          :handleUpdate="handleLastTableUpdate"
          :handleDelete="lastHandleDelete"
        />
        <pagination
          v-show="total>0 && isUnderlying !== '0'"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改非底层分类管理对话框 -->
    <commonDialog
      :title="title"
      :open="open"
      :form="form"
      :treeData="treeData"
      :defaultProps="defaultProps"
      :handleFormInfoNodeClick="handleFormInfoNodeClick"
      :tableOptions="tableOptions"
      :nodeDataNo="nodeDataNo"
      :cancel="cancel"
      :getList="getList"
      :getTree="getTree"
      :closeOpen="closeOpen"
    />
    <!--  添加底层表信息弹出层  -->
    <lastDialog
      :tableInfoTitle="tableInfoTitle"
      :tableInfoOpen="tableInfoOpen"
      :tableInfoForm="tableInfoForm"
      :getList="getList"
      :closeTableInfo="closeTableInfo"
    />
    <!-- 表关系弹出层 -->
    <tableRelationDialog
      :tableRelationDialogVisible="tableRelationDialogVisible"
      @closeTRelationDia="closeTRelationDia"
      :tableRelationTitle="tableRelationTitle"
      :TableNameOptions="TableNameOptions"
      :queryParams="queryParams"
      :tableRelationForm="tableRelationForm"
      :getTableRelation="getTableRelation"
    />
    <!-- 数据结构弹出层   -->
    <dataStructureDialog
      :dataStructureDialogVisible="dataStructureDialogVisible"
      :dataStructureTitle="dataStructureTitle"
      @closeDStructure="closeDStructure"
      :queryParams="queryParams"
      :getStruct="getStruct"
      :dataStructureForm="dataStructureForm"
    />
    <!-- 存储设置弹出层   -->
    <storeSettingsDialog
      :storeSettingsDialogVisible="storeSettingsDialogVisible"
      :storeSettingsTitle="storeSettingsTitle"
      @closeSSettings="closeSSettings"
      :queryParams="queryParams"
      :getSave="getSave"
      :selectFormat="SaveSettingsOptions"
      :storeSettingsForm="storeSettingsForm"
    />
    <!--  档号规则弹出层  -->
    <NoRulesDialog
      :NoRulesDialogVisible="NoRulesDialogVisible"
      :NoRulesTitle="NoRulesTitle"
      :selectFormat="NoRulesOptions"
      :queryParams="queryParams"
      @closeNoRules="closeNoRules"
      :getArNoPolicy="getArNoPolicy"
      :arNoPolicy="arNoPolicy"
    />
    <!-- 鉴定规则弹出层   -->
    <IdRulesDialog
      :IdRulesDialogVisible="IdRulesDialogVisible"
      :IdRulesForm="IdRulesForm"
      @closeIdRules="closeIdRules"
      :IdRulesTitle="IdRulesTitle"
      :queryParams="queryParams"
      :scopeList="scopeList"
      :securityList="securityList"
      :filter1="filter1"
      :filter2="filter2"
      :filter4="filter4"
      :getIdRulesList="getIdRulesList"
      :timeRange.sync="timeRange"
    />
  </div>
</template>

<script>
import {
  exportSort,
  getSearchList,
} from '@/api/system/sort'
import lastTables from '@/views/sorts/tablesorts/components/lastTables'
import commonTables from '@/views/sorts/tablesorts/components/commonTables'
import commonDialog from '@/views/sorts/tablesorts/components/commonDialog'
import lastDialog from '@/views/sorts/tablesorts/components/lastDialog'
import rightBtns from '@/views/sorts/tablesorts/components/rightBtns'
import tableRelationDialog from '@/views/sorts/tablesorts/components/tableRelationDialog'
import dataStructureDialog from '@/views/sorts/tablesorts/components/dataStructureDialog'
import storeSettingsDialog from '@/views/sorts/tablesorts/components/storeSettingsDialog'
import NoRulesDialog from '@/views/sorts/tablesorts/components/NoRulesDialog'
import IdRulesDialog from '@/views/sorts/tablesorts/components/IdRulesDialog'
import {list} from '@/views/sorts/tablesorts/mixin/getList'
import {add} from '@/views/sorts/tablesorts/mixin/add'
import {update} from '@/views/sorts/tablesorts/mixin/update'
import {close} from '@/views/sorts/tablesorts/mixin/close'
import {del} from '@/views/sorts/tablesorts/mixin/del'

export default {
  name: "Sort",
  components:{
    lastTables,
    commonTables,
    commonDialog,
    rightBtns,
    lastDialog,
    tableRelationDialog,
    dataStructureDialog,
    storeSettingsDialog,
    NoRulesDialog,
    IdRulesDialog
  },
  data() {
    return {
      //树形数据
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      isUnderlying:false,// 是否底层
      nodeData:"",//选中树节点
      nodeDataNo:"",
      loading: true,// 遮罩层
      ids: [],// 选中数组
      sNames: [],
      single: true,// 非单个禁用
      multiple: true,// 非多个禁用
      showSearch: true,// 显示搜索条件
      total: 0,// 总条数
      sortList: [],// 分类管理表格数据
      lastSortList:[],// 底层表格数据
      timeRange:[],
      title: "",// 弹出层标题
      tableInfoTitle:"添加表信息",
      open: false,// 是否显示弹出层
      tableInfoOpen:false,
      addAvail: false, //鉴定规则页面，规则只允许存在一个，在高延迟加载中或加载失败时禁用新增按钮防止添加多个规则
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortMainTableNo: null,
        isLast:"1",
        sortPrantNo:"3",
        sortName:""
      },
      formParams:{
        sortName:"",
        sortMainTableNo:'',
      },
      form: {},// 表单参数
      tableInfoForm:{ // 添加表信息参数
        tableName:"",
        tableAlias:"",
        orderNo:"",
        tableType:"0",
        recNo:""
      },
      tableOptions:[],// 表选项
      tableLabel:[],
      orderNos:[],//顺序排序数组
      lastTableLoading:false,
      tableRelationDialogVisible:false,//表关系弹出层
      tableRelationTitle:"添加表关系",
      tableRelationForm:{},
      TableNameOptions:[], // 表关系表名列表
      dataStructureDialogVisible:false,// 数据结构弹出层
      dataStructureTitle:'添加数据结构',
      dataStructureForm:{
        structName:'',
        structCode:'',
        isOpen:'1',
        remark:''
      },
      storeSettingsDialogVisible:false,// 存储设置弹出层
      storeSettingsTitle:'添加存储设置',
      SaveSettingsOptions:[],
      storeSettingsForm:{
        policyName:'',
        fileFsmt:'',
        isOpen:'',
        remark:''
      },
      NoRulesDialogVisible:false,//档号规则弹出层
      NoRulesTitle:'添加档号规则',
      NoRulesOptions:[],
      arNoPolicy:{
        policyName:'',
        fileFsmt:'',
        isOpen:'1',
        remark:''
      },
      IdRulesDialogVisible:false,//鉴定规则弹出层
      IdRulesTitle:'添加鉴定规则',
      IdRulesForm:{
        authenticateName:'',
        sortNo:'',
        cate:'',
        pattern:'',
        patternType:'',
        patternText:'',
        scope:'',
        securityLevel:'',
        isOpen:'1',
        remark:'',
      },
      IdRuleList:[],
      scopeList:[],//公开范围字典接口
      securityList:[],//密级字典接口
      filter1:[],// 过滤条件1
      filter2:[
        {
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }
      ],// 过滤条件2
      filter4:[
        {
          value: '选项1',
          label: '黄金糕'
        }, {
          value: '选项2',
          label: '双皮奶'
        }, {
          value: '选项3',
          label: '蚵仔煎'
        }, {
          value: '选项4',
          label: '龙须面'
        }, {
          value: '选项5',
          label: '北京烤鸭'
        }
      ],// 过滤条件4
    }
  },
  created() {
    this.getList()
    this.getTree()
    this.getTableTitle()
    this.gettableNameOptions()
    this.getArNoPolicyOptions()
    this.getScope()
    this.getSecurity()
    this.loadFilterConditions()
    this.loadFConditions()
    this.getTime()
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        recNo: null,
        sortName: null,
        sortPrantName: null,
        sortMainTableNo: null,
        sortViewTableNo: null,
        sortArcNoCnt: null,
        isLast: "1",
        delFlag: "0",
        orderNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        sortSublist: null
      };
      this.resetForm("form");
    },
    // 搜索按钮操作
    async handleQuery() {
      const params = {}
      params.sortName = this.formParams.sortName
      if(this.formParams.sortMainTableNo){
        params.sortMainTableNo = this.formParams.sortMainTableNo.join(",")
      }
      const res = await getSearchList(params)
      this.sortList = res.rows
      this.isUnderlying = '1'
      // this.queryParams.sortMainTableNo = ""
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recNo)
      this.sNames = selection.map(item => item.sortName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 导出按钮操作
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有分类管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSort(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //点击树形节点
    handleNodeClick(data) {
      if(data.recNo == 4) {
        this.addAvail = false;
      }
      this.isUnderlying = data.last || 22
      this.nodeData = data.label
      this.nodeDataNo = data.rec
      this.queryParams.sortPrantNo = data.recNo
      this.IdRulesForm.cate = data.sortName
      this.queryParams.isLast = data.isLast
      this.queryParams.sortName = data.sortName
      this.queryParams.sortMainTableNo = data.sortMainTableNo
      if(data.last === '0'){
        this.$message.info('请点击下级分类')
      }
      switch (data.rec){
        case "001":
          /* 右侧表头：
              表信息：表名称、表别名、表类型、操作
              表关系：表关系名称、主表表名、子表表名、关系字段、操作
              数据结构：结构名称、结构简码、备注、操作
              存储设置：模板名称、启用状态、备注、操作
              档号规则：规则名称、启用状态、备注、操作
              鉴定规则：规则名称、启用状态、备注、操作 */
          this.tableLabel= [
            {label: '表名称', width: '', prop: 'tableName',align:'left'},
            {label: '表别名', width: '200', prop: 'tableAlias',align:'left'},
            {label: '表类型', width: '', prop: 'tableType'},
            {label: '操作', width: '', prop: 'phone',isOpera: true,showTableField:true,showUpdate:true,showDel:true},
          ]
          this.getList()
          break
        case "002":
          this.tableLabel= [
            {label: '表关系名称', width: '', prop: 'relationName'},
            {label: '主表表名', width: '', prop: 'parentNo'},
            {label: '子表表名', width: '', prop: 'childNo'},
            {label: '关系字段', width: '', prop: 'Filed'},
            {label: '操作', width: '', prop: 'area',isOpera: true,showUpdate:true,showDel:true}
          ]
          this.getTableRelation(data.recNo);
          break
        case "003":
          this.tableLabel= [
            {label: '结构名称', width: '', prop: 'structName'},
            {label: '结构简码', width: '', prop: 'structCode'},
            //{label: '是否启用', width: '', prop: 'isOpen', isOpera: true,showSwitch:true}, 
            {label: '备注', width: '', prop: 'remark'},
            {label: '操作', width: '', prop: 'area',isOpera: true,showUpdate:true,showDel:true}
          ]
          this.getStruct(data.recNo)
          break
        case "004":
          this.tableLabel= [
            {label: '模板名称', width: '', prop: 'policyName'},
            {label: '备注', width: '', prop: 'remark'},
            {label: '操作', width: '', prop: 'area',isOpera: true,showUpdate:true,showDel:true}
          ]
          this.getSave(data.recNo)
          this.getSaveSettingsOptions(data)
          break
        case "005":
          this.tableLabel= [
            {label: '规则名称', width: '', prop: 'policyName'},
            {label: '启用状态', width: '', prop: 'isOpen'},
            {label: '备注', width: '', prop: 'remark'},
            {label: '操作', width: '', prop: 'area',isOpera: true,showUpdate:true,showDel:true}
          ]
          this.getArNoPolicy(data.recNo)
          break
        case "006":
          this.tableLabel= [
            {label: '规则名称', width: '', prop: 'authenticateName'},
            {label: '启用状态', width: '', prop: 'isOpen'},
            {label: '备注', width: '', prop: 'remark'},
            {label: '操作', width: '', prop: 'area',isOpera: true,showUpdate:true,showDel:true}
          ]
          this.getIdRulesList(data.recNo)
          this.getFieldQuery()
          break
        default:
          this.getList()
      }

    },
    // 底层点击树节点
    handleFormInfoNodeClick(data){
      this.form.sortPrantNo = data.recNo
      this.form.sortPrantName = data.label
    },
  },
  mixins:[list,add,update,close,del]
};
</script>

<style lang="scss" scoped>
.sorts-container{
  display: flex;
  flex-direction: column;
  padding: 0 20px;
  .top-form{
    height: 80px;
    display: flex;
    align-items: center;
  }
  .bottom-main{
    display: flex;
    .left-tree{
      flex: 1;
      margin-right: 20px;
      height: 700px;
      overflow: auto;
    }
    .right-table{
      flex: 5;
    }
  }
}
/* 设置滚动条的样式 */
::-webkit-scrollbar {
  width:10px;
}
/*定义滚动条的轨道颜色、内阴影及圆角*/
::-webkit-scrollbar-track{
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
  border-radius: 3px;
}
/*定义滑块颜色、内阴影及圆角*/
::-webkit-scrollbar-thumb{
  border-radius: 7px;
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
  background-color: #E8E8E8;
}
</style>
